
var app = (function (Constants) {
    var hostMis = Constants.getCommonParameterConstant('HOST_MIS_RELEASE');
    var hostB2b = Constants.getCommonParameterConstant('HOST_B2B_RELEASE');
    var baseArgsDataConfig = {
        'appkey': Constants.getCommonParameterConstant('APPKEY'),
        'versionCode': Constants.getCommonParameterConstant('VERSION_CODE'),
        'type': Constants.getCommonParameterConstant('TYPE'), //应用类型：1-ios ，2-android，3-js web
    }

    var _getData = function (data, callback) {
        if (data == null) {
            data = {};
        }
        var timestamp = Date.parse(new Date());
        data.appkey = baseArgsDataConfig.appkey;
        data.versionCode = baseArgsDataConfig.versionCode;
        data.randomCode = timestamp;
        data.type = baseArgsDataConfig.type;
        data.callbackfun = callback;
        return data;
    };

    function jsonp(host, url, data, callback, error) {
        var opt;
        if (arguments.length == 3) {
            opt = _getCommonOpt(host, url, data, null, error);
        } else {
            opt = _getCommonOpt(host, url, data, callback, error);
        }
        opt.type = 'GET';
        opt.dataType = 'JSONP';
        opt.contentType = 'application/x-www-form-urlencoded; charset=UTF-8';

        return _sendRequst(opt);
    }

    function cors(){}

    function jsonpMis(url, data, callback) {
        jsonp(hostMis, url, data, callback, null);
    }

    function jsonpB2b(url, data, callback) {
        jsonp(hostB2b, url, data, callback, null);
    }

    function _getCommonOpt(host, url, data, callback) {
        var data = _getData(data, callback);
        return {
            url: host + url,
            data: data
        }
    }

    function _sendRequst(opt) {
        var obj = {
            url: opt.url,
            type: opt.type,
            data: opt.data,
            dataType: opt.dataType,
            contentType: opt.contentType,
            timeout: opt.timeout || 50000,
            beforeSend: function (xhr) { },
            success: function (res) {
                opt.callback(res);
            },
            error: function (err) {
                opt.error && opt.error(err);
            }

        }
        return $.ajax(obj);
    }

    return {
        get: jsonpMis,
        getShop: jsonpB2b
    };

})(Constants);